package com.lagou.edu.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.lagou.edu.pojo.DataConfig;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.sql.SQLException;

/**
 * @author 应癫
 */
public class DruidUtils {

   /* private DruidUtils(){
    }*/

   // private static DruidDataSource druidDataSource = new DruidDataSource();
    private static ZkClient zkClient=ZookeeperUtil.getZkClient();

    static {
        if(!zkClient.exists(ZookeeperUtil.ROOT_PATH)) {
            DataConfig dataConfig = new DataConfig();
            dataConfig.setDriverClassName("com.mysql.jdbc.Driver");
            dataConfig.setUrl("jdbc:mysql://localhost:3306/bank?autoReconnect=true&useSSL=false&characterEncoding=utf-8");
            dataConfig.setUsername("root");
            dataConfig.setPassword("root");
            zkClient.createPersistent(ZookeeperUtil.ROOT_PATH,dataConfig);

        }
        //DataConfig dataConfig = zkClient.readData(ZookeeperUtil.ROOT_PATH);

       // DruidDataSource druidDataSource = new DruidDataSource();



    }



    public static DruidDataSource getInstance(DataConfig dataConfig) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(dataConfig.getDriverClassName());
        druidDataSource.setUsername(dataConfig.getUsername());
        druidDataSource.setPassword(dataConfig.getPassword());
        druidDataSource.setUrl(dataConfig.getUrl());
        druidDataSource.setTestOnBorrow(false);

        return druidDataSource;
    }
    public static ZkClient getZkClient(){
        return zkClient;
    }
}
